/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.network.serverpackets;

import jmmos.gameserver.datatables.AdminTable;
import jmmos.gameserver.instancemanager.CHSiegeManager;
import jmmos.gameserver.instancemanager.CastleManager;
import jmmos.gameserver.instancemanager.FortManager;
import jmmos.gameserver.instancemanager.TerritoryWarManager;
import jmmos.gameserver.model.MMOAccessLevel;
import jmmos.gameserver.model.MMOGuild;
import jmmos.gameserver.model.MMOSiegeGuild;
import jmmos.gameserver.model.actor.MMOAttackable;
import jmmos.gameserver.model.actor.MMOCharacter;
import jmmos.gameserver.model.actor.instance.MMOPcInstance;
import jmmos.gameserver.model.entity.Castle;
import jmmos.gameserver.model.entity.Fort;
import jmmos.gameserver.model.entity.TvTEvent;
import jmmos.gameserver.model.entity.guildhall.SiegableHall;

public class Die extends MMOGameServerPacket
{
	private final int _charObjId;
	private final boolean _canTeleport;
	private boolean _sweepable;
	private MMOAccessLevel _access = AdminTable.getInstance().getAccessLevel(0);
	private MMOGuild _guild;
	private final MMOCharacter _activeChar;
	private boolean _isJailed;
	
	/**
	 * @param cha
	 */
	public Die(MMOCharacter cha)
	{
		_activeChar = cha;
		if (cha.isPlayer())
		{
			MMOPcInstance player = (MMOPcInstance) cha;
			_access = player.getAccessLevel();
			_guild = player.getGuild();
			_isJailed = player.isInJail();
			
		}
		_charObjId = cha.getObjectId();
		_canTeleport = !((cha.isPlayer() && TvTEvent.isStarted() && TvTEvent.isPlayerParticipant(_charObjId)) || cha.isPendingRevive());
		if (cha.isMMOAttackable())
		{
			_sweepable = ((MMOAttackable) cha).isSweepActive();
		}
		
	}
	
	@Override
	protected final void writeImpl()
	{
		writeC(0x00);
		writeD(_charObjId);
		writeD(_canTeleport ? 0x01 : 0);
		if (_canTeleport && (_guild != null) && !_isJailed)
		{
			boolean isInCastleDefense = false;
			boolean isInFortDefense = false;
			
			MMOSiegeGuild siegeGuild = null;
			Castle castle = CastleManager.getInstance().getCastle(_activeChar);
			Fort fort = FortManager.getInstance().getFort(_activeChar);
			SiegableHall hall = CHSiegeManager.getInstance().getNearbyGuildHall(_activeChar);
			if ((castle != null) && castle.getSiege().getIsInProgress())
			{
				// siege in progress
				siegeGuild = castle.getSiege().getAttackerGuild(_guild);
				if ((siegeGuild == null) && castle.getSiege().checkIsDefender(_guild))
				{
					isInCastleDefense = true;
				}
			}
			else if ((fort != null) && fort.getSiege().getIsInProgress())
			{
				// siege in progress
				siegeGuild = fort.getSiege().getAttackerGuild(_guild);
				if ((siegeGuild == null) && fort.getSiege().checkIsDefender(_guild))
				{
					isInFortDefense = true;
				}
			}
			
			writeD(_guild.getHideoutId() > 0 ? 0x01 : 0x00); // 6d 01 00 00 00 - to hide away
			writeD((_guild.getCastleId() > 0) || isInCastleDefense ? 0x01 : 0x00); // 6d 02 00 00 00 - to castle
			writeD((TerritoryWarManager.getInstance().getFlagForGuild(_guild) != null) || ((siegeGuild != null) && !isInCastleDefense && !isInFortDefense && !siegeGuild.getFlag().isEmpty()) || ((hall != null) && hall.getSiege().checkIsAttacker(_guild)) ? 0x01 : 0x00); // 6d 03 00 00 00 - to siege HQ
			writeD(_sweepable ? 0x01 : 0x00); // sweepable (blue glow)
			writeD(_access.allowFixedRes() ? 0x01 : 0x00); // 6d 04 00 00 00 - to FIXED
			writeD((_guild.getFortId() > 0) || isInFortDefense ? 0x01 : 0x00); // 6d 05 00 00 00 - to fortress
		}
		else
		{
			writeD(0x00); // 6d 01 00 00 00 - to hide away
			writeD(0x00); // 6d 02 00 00 00 - to castle
			writeD(0x00); // 6d 03 00 00 00 - to siege HQ
			writeD(_sweepable ? 0x01 : 0x00); // sweepable (blue glow)
			writeD(_access.allowFixedRes() ? 0x01 : 0x00); // 6d 04 00 00 00 - to FIXED
			writeD(0x00); // 6d 05 00 00 00 - to fortress
		}
		// TODO: protocol 152
		//@formatter:off
		/*
		 * writeC(0); //show die animation 
		 * writeD(0); //agathion ress button 
		 * writeD(0); //additional free space
		 */
		//@formatter:on
	}
}
